var app = getApp()
// pages/order_list/order_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delBtnWidth: 170,
    firstRow: 0,
    store_list: '',
    k: '',
    u: '',
    show:0,
    request_num:0,
    is_show_time: 1

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  收藏店铺
    var that = this
    var k = wx.getStorageSync('key')
    if (k) {
      that.requests()
    } else {
      this.setData({
        is_show_time: 0,
      })
    }
   
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.store_list.forEach(function (v, i) {
      if (v.isTouchMove)//只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      store_list: this.data.store_list
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    that.data.store_list.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      store_list: that.data.store_list
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  //取消收藏商品
  bindViewCancel: function (e) {
   
    var that = this
    var id = e.currentTarget.dataset.id
  
    wx.showModal({
      title: '取消收藏',
      content: '确认取消收藏吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Favorites&met=delFavoritesShop&typ=json',
            method: 'GET',
            data: { id: id, u: that.data.u, k: that.data.k },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              app.show('取消收藏成功', 'success')
              that.requests()
            }
          })
        }
      }
    })
  },
  //数据请求
  requests: function () {
    var that = this
    var k = wx.getStorageSync('key')
    var u = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Favorites&met=favoritesShop&typ=json',
      method: 'GET',
      data: { k: k, u: u, curpage: that.data.curpage, page: that.data.page },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        
        if (res.data.status == 200) {
          
          if (res.data.data.items){
            var store_list = res.data.data.items
          }else{
            var store_list = ''
          }
          that.setData({
            store_list: store_list,
            k: k,
            u: u,
            is_show_time: 0
          })
        }
      }
    })
  },

  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    var that = this
    var firstRow = that.data.firstRow + 10
    if (that.data.request_num == 1) {
      return false
    }
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Favorites&met=favoritesShop&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, firstRow: firstRow },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status == 200) {
          if (res.data.data.items.length > 0) {
            var store_list = that.data.store_list
            for (var z in res.data.data.items) {
              store_list.push(res.data.data.items[z])
            }

            that.setData({
              store_list: store_list,
              firstRow: firstRow
            })
          } else {
            that.setData({
              request_num: 1
            })
          }
        }
      }
    })
  },


  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {
       
        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    var that = this
    wx.showToast({
      title: '加载中',//提示信息
      icon: 'loading',//成功显示图标
      duration: 2000//时间
    })
    wx.stopPullDownRefresh()
    that.onLoad()
  },
})